/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.DonGiaPOJO;

/**
 *
 * @author b2
 */
public class DonGiaDAO {

    public static ArrayList<DonGiaPOJO> layDanhSachDonGia() {

        ArrayList<DonGiaPOJO> ds = new ArrayList<DonGiaPOJO>();
        try {
            String sql = "SELECT * FROM DONGIA ";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                DonGiaPOJO dg = new DonGiaPOJO();
                dg.setMaDonGia(rs.getString("MaDonGia"));
                dg.setMaTuyenBay(rs.getString("MaTuyenBay"));
                dg.setMaHangVe(rs.getString("MaHangVe"));
                dg.setDonGia(rs.getDouble("DonGia"));
                ds.add(dg);
            }
            helper.close();
        } catch (SQLException ex) {
            Logger.getLogger(DonGiaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ds;
    }

    public static DonGiaPOJO layDonGia(String maDonGia) {
        DonGiaPOJO dg = new DonGiaPOJO();
        try {
            String sql = "SELECT * FROM DONGIA WHERE MADONGIA = '" + maDonGia + "'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                dg.setMaDonGia(rs.getString("MaDonGia"));
                dg.setMaTuyenBay(rs.getString("MaTuyenBay"));
                dg.setMaHangVe(rs.getString("MaHangVe"));
                dg.setDonGia(rs.getDouble("DonGia"));
            }
            helper.close();
        } catch (SQLException ex) {
            Logger.getLogger(DonGiaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dg;
    }

    public static boolean themDonGia(DonGiaPOJO dg) {
        boolean kq = false;
        String sql = String.format("INSERT INTO DONGIA (`MaDonGia`,`MaTuyenBay`,`MaHangVe`,`DonGia`) VALUES('%s','%s','%s','%s');", dg.getMaDonGia(), dg.getMaTuyenBay(), dg.getMaHangVe(), dg.getDonGia());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();

        return kq;
    }
}
